/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.Properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author RAHMAN
 */
public class Loader {

    private String nameFile = null;
    Properties file = null;

    public Loader() {
        setNameFile("config.properties");
        file = LoadProperties(getNameFile());
    }

    public Properties getFile() {
        return file;
    }

    public String getNameFile() {
        return nameFile;
    }

    public void setNameFile(String nameFile) {
        this.nameFile = nameFile;
    }

    public static Properties LoadProperties(String name) {
        FileInputStream in = null;
        Properties tem = new Properties();
        try {
            File cek = new File(name);
            if (!cek.exists()) {
            //    JOptionPane.showMessageDialog(null, "File Not Found", "Confirmation", JOptionPane.ERROR_MESSAGE);
                System.out.println("Properties Not Found..................");
                //                System.exit(1);
                createProperties(name);
                System.out.println("File created");
            }
            in = new FileInputStream(name);
            tem.load(in);
            return tem;
        } catch (IOException ex) {
            Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tem;
    }

    public void getProp() {
    }

    public static void createProperties(String file) {
        FileOutputStream out = null;
        try {
            Properties data = new Properties();
            out = new FileOutputStream(file);
//            //Untuk Setting Database....................
//            //data.setProperty("DRIVER_SIGAWE", "com.mysql.jdbc.Driver");
//            data.setProperty("DBMS_NAME", "mysql");
//            data.setProperty("DATABASE_NAME", "gateway");
//            data.setProperty("DATABASE_HOST", "localhost");
//            data.setProperty("DATABASE_USER", "SiGawe");
//            data.setProperty("DATABASE_PASSWORD", "SiGaweGawe");
//            data.setProperty("DATABASE_PORT", "3306");
//            //Untuk Settingan Server........................
//            data.setProperty("SERVER_NAME", "sigawe.net");
//            data.setProperty("SERVER_PORT", "5222");
//            data.setProperty("USER_NAME_GATEWAY", "4321");
//            data.setProperty("PASSWORD_GATEWAY", "4321");
//            data.store(out, " ===================================\n Konfigurasi siGaweClient \n ============================");
            //Untuk Setting Database....................
            data.setProperty("DRIVER_SIGAWE", "com.mysql.jdbc.Driver");
            data.setProperty("DBMS_NAME", "mysql");
            data.setProperty("DATABASE_NAME", "sigawe");
            data.setProperty("DATABASE_HOST", "localhost");
            data.setProperty("DATABASE_USER", "sigawe");
            data.setProperty("DATABASE_PASSWORD", "s1gaw3");
            data.setProperty("DATABASE_PORT", "3306");
            //Untuk Settingan Server........................
            data.setProperty("SERVER_NAME", "macan.uad.ac.id");
            data.setProperty("SERVER_PORT", "4441");
            data.setProperty("USER_NAME_GATEWAY", "1312");
            data.setProperty("PASSWORD_GATEWAY", "1312");
            data.store(out, " ===================================\n Konfigurasi siGaweClient \n ============================");
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
